#include <stdio.h>
#include <stdlib.h>
int D[2][13] = { { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, { 0, 31,
		29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } };
int isleap(int y) {
	return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
}
int main(void) {
	int N, y, m, d;
	scanf("%d", &N);
	while (N--) {
		scanf("%d%d%d", &y, &m, &d);
		int s = d, leap = isleap(y), i;
		for (i = 1; i < m; i++)
			s += D[leap][i];
		printf("%d\n", s);
	}
	return EXIT_SUCCESS;
}
